#include <all_wlib.h>
#pragma hdrstop
//#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma link "HWindow"
#pragma link "ozf_Base"
#pragma link "HRectButton"

#include "ozf_Compas.h"

DEF_SWITCH( Compas )

//---------------------------------------------------------------------------
__fastcall TCompasForm::TCompasForm(TComponent* Owner)
    : TOziBaseForm(Owner)
  {
     FColorLight = -1;
     FAngle      = 0;
     panImg->DoubleBuffered = true;

//CFG
     Init( "Angle", (TForm**)&Form );
     ColorLight           = GOPT( "Angle/Color",    10 );
     panControls->Visible = GOPT( "Angle/CVisible", panControls->Visible );
}

void __fastcall TCompasForm::idPos( double Lat,double Lon,double vSpeed,double Head,double Mag,double Alt, int x, int y )
  {
    Angle = Head;
    image->Invalidate();
}

DECL_PROP_SET( TCompasForm, int, ColorLight )
  {
    if ( FColorLight == val ) return;
    FColorLight = val;

    pgColor->Position = val;
    image->Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCompasForm::FormClose(TObject *Sender, TCloseAction &Action)
  {
//CFG
     SOPT( "Angle/Color",    ColorLight );
     SOPT( "Angle/CVisible", panControls->Visible );
  //Base
     TOziBaseForm::FormClose( Sender, Action );
}
//---------------------------------------------------------------------------
void __fastcall TCompasForm::pgColorChange(TObject *Sender)
  {
    ColorLight = pgColor->Position;
}
//---------------------------------------------------------------------------
void __fastcall TCompasForm::idColorLess(TObject *Sender)
  {
    pgColor->Position = pgColor->Position-1;
}
//---------------------------------------------------------------------------
void __fastcall TCompasForm::idColorMore(TObject *Sender)
{
    pgColor->Position = pgColor->Position+1;
}
//---------------------------------------------------------------------------
void __fastcall TCompasForm::idOnButton( TObject *Sender, const PHCustomWindowBorder Border, const HCaptionButton_t Button )
{
     if ( Button == hcbHelp )
       panControls->Visible = !panControls->Visible;
     TOziBaseForm::idOnButton( Sender, Border, Button );
}
//---------------------------------------------------------------------------
void __fastcall TCompasForm::FormKeyDown( TObject *Sender, WORD &Key, TShiftState Shift )
  {
     if ( Key == VK_ADD )      idColorMore(NULL); else
     if ( Key == VK_SUBTRACT ) idColorLess(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TCompasForm::FormKeyPress(TObject *Sender, char &Key)
  {
     if ( Key == '+' || Key == '=' ) idColorMore(NULL); else
     if ( Key == '-' )               idColorLess(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TCompasForm::imagePaint(TObject *Sender)
  {  TCanvas *dc = image->Canvas;
     MyRect   r( 0,0,image->Width,image->Height );
     TColor   cl = (TColor)RGB( 0,ColorLight*16-1,0 );
     int      w;

//Fill
     TC_Fill( dc,r,clBlack );

//Circle
     dc->Pen->Width = 1;
     w = Min( image->Width,image->Height ) / 2;
     r.Set( image->Width/2 - w + 10, image->Height/2 - w +10,
            image->Width/2 + w - 10, image->Height/2 + w - 10 );

     dc->Pen->Style = psSolid;
     TC_Ellipse( dc, r, cl );

     dc->Pen->Style = psDot;
     TC_Line( dc, r.x-10, r.CenterY(), r.x1+10, r.CenterY(), cl );
     TC_Line( dc, r.CenterX(), r.y-10, r.CenterX(), r.y1+10, cl );

//Text
     cl = (TColor)RGB( ColorLight*16-1,ColorLight*16-1,0 );
     dc->Font->Color = cl;
     TC_TextOut( dc, 2, 2, Message( "%2.1lf", Angle ) );

//Pointer
     dc->Pen->Style = psSolid;
     dc->Pen->Width = 3;
     cl = (TColor)RGB( ColorLight*16-1,ColorLight*16-1,0 );
     TC_Line( dc,
              r.CenterX(), r.CenterY(),
              r.CenterX() + w*cos((Angle-90)*M_PI/180),
              r.CenterY() + w*sin((Angle-90)*M_PI/180),
              cl );
}
//---------------------------------------------------------------------------
